//@ sourceURL=privileges.js
function menus(userid){
	$("#titleDom").html()
	$(".radioDom").remove()
	$("#checkBox").html()
	$.ajax({
		type : "post",
		url :  "staff/geUsermenus",
		dataType : "text",
		data : {"userid":userid},
		success : function(res) {
			var strs= new Array();
			strs= res.split(",");
			var json;
			console.log(strs)
			$.post(ctx + "staff/menus", {}, function (r) {
				if(r.code==0){
//				    var ref = $('#checkBox').jstree(true);
//				    var deptIds = ref.get_checked();
//				    $("[name='parentId']").val(deptIds[0]);
					var data=r.msg;
					var menus=data.children;
					console.log(menus);
					var oStitle='';
					var oSson='';
					var oSsons='';
					for(var i=0;i<menus.length;i++){
						if(menus[i].id!=120){
							oStitle='<div class="privilegeMenu">'+menus[i].text+'</div>';
							if(i != 5){
								oSson='<div class="radioDom"><div style="width:102px;margin:0 auto;text-align:left;" id="id'+i+'"></div></div>';
							}else{
								oSson='<div class="radioDom"><div style="width:120px;padding-left: 9px;margin:0 auto;text-align:left;" id="id'+i+'"></div></div>';
							}
							$("#titleDom").append(oStitle);
							$("#checkBox").append(oSson);
							for(var j=0;j<menus[i].children.length;j++){
								var s1='<div class="menuFather"><input class="menusid_input" type="checkbox" name="menusid"';
								for(var k=0;k< strs.length;k++){
									s3="";
									if(strs[k]==menus[i].children[j].id){
										if(userid != null && userid !=""){
											s3='checked="checked"'
										}
											break;
									}
								}
								//页面隐藏待支付和已取消订单权限
								var s2='';
								if(menus[i].children[j].text=="待支付"||menus[i].children[j].text=="已取消订单"){
									continue;
								}else{
									s2='id="id'+menus[i].children[j].id+'" value="'+menus[i].children[j].id+'" /><div class="menuChildren">'+menus[i].children[j].text+'</div></div><div style="margin-left:28px;" id="ids'+menus[i].children[j].id+'"></div>';
								}
								//var s2='id="id'+menus[i].children[j].id+'" value="'+menus[i].children[j].id+'" /><div class="menuChildren">'+menus[i].children[j].text+'</div></div><div style="margin-left:28px;" id="ids'+menus[i].children[j].id+'"></div>';
								oSsons=s1+s3+s2;
								$("#id"+i).append(oSsons);
								for (var int = 0; int < menus[i].children[j].children.length; int++) {
									var s5='<div class="menuSon"><input class="menusid_input" type="checkbox" name="menusid"';
										for(var k=0;k< strs.length;k++){
											s7="";
											if(strs[k]==menus[i].children[j].children[int].id){
												if(userid != null && userid !=""){
													s7='checked="checked"'
												}
													break;
											}
										}
									var s6='id="id'+menus[i].children[j].children[int].id+'" value="'+menus[i].children[j].children[int].id+'" /><div class="menuChildren">'+menus[i].children[j].children[int].text+'</div></div>';
									$("#ids"+menus[i].children[j].id).append(s5+s7+s6);
								}
							}
						}
					}
				}
			});	
		},
		error : function() {
		}
	});
	
}

		var  results=[];
		function save(){
			var  notcheckOrderCodeInfoItem=$("input:checked").each(function(i){
		});
		results=[];
		for(var o=1;o<notcheckOrderCodeInfoItem.length;o++){
			results.push({
					"id":notcheckOrderCodeInfoItem[o].id.slice(2),
					"check":true
				})
			}
			console.log(results)
		}
$(function(){
	jiazai();
})
function jiazai(){
	var obj = $("#userid").length;
	var userid;
	console.log(obj)
	if(obj>0){
		userid=$("#userid").val();
		$("#userid").remove();
		$("#uid").val(userid);
	}else{
		userid=$("#uid").val();
	}
	$("#mobile").val($("#usermo").val());
	$("#checkBox").html("")
	$("#titleDom").html("")
	$("#userid1").val(userid);
	getuser(userid);
	setsex();
}
function dianji(id){
		$("#uid").val(id);
		$.ajax({
			type : "post",
			url :  "staff/geUser",
			dataType : "json",
			data : {"userid":id},
			success : function(res) {
				$("#uid").val(id);
				$("#mobile").val(res.mobile);
				$("#usersex").val(res.ssex);
				$("#username").val(res.username);
				//$("#hotelId").val(res.hotelId);
				//$("#hotelName").val(res.hotelName);
				$("#position").val(res.position);
				//menus(id);
				setsex();
			},
			error : function() {
			}
		});
	jiazai();
}
function setsex(){
	 var usersex=$("#usersex").val();
     var sexObj = $("input:radio[name='sex']");
     sexObj.each(function () {
         if($(this).val() == usersex) {
            $(this).attr("checked", "checked");
         }
     });
}
function getuser(userid){
	$("#checkBox").html("")
	$("#titleDom").html("")
	$.ajax({
		type : "post",
		url :  "staff/geUser",
		dataType : "json",
		data : {"userid":userid},
		success : function(res) {
			$("#username").val(res.username);
			$("#hotelId").val(res.hotelId);
			$("#hotelName").val(res.hotelName);
			$("#position").val(res.position);
			menus(userid);
		},
		error : function() {
		}
	});
}

function save(){
	 var userid = $("#uid").val();
	 var hotelId= $("#hotelId").val();
	 var mobile = $("#mobile").val();
	 var ssex = $("input:radio[name='sex']:checked").val();
	 var position = $("#position").val();
	 var mids
	 $('input[name="menusid"]:checked').each(function(){  
		 mids=$(this).val()+","+mids;//向数组中添加元素  
	 });
		 $.ajax({
				type : "post",
				url :  "staff/save",
				dataType : "json",
				data : {"userid":userid,"mids":mids,"hotelId":hotelId,"mobile":mobile,"ssex":ssex,"position":position},
				success : function(res) {
					$("#titleDom").html("")
					$("#checkBox").html("")
					menus(userid);
				},
				error : function() {
				}
			});
}
